package com.zhuojy.equity.mall.admin.service.message;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.TAuditCenter;
import com.zhuojy.equity.mall.admin.common.entity.TPosition;
import com.zhuojy.equity.mall.admin.common.entity.bo.TPositionMessageBO;
import com.zhuojy.equity.mall.admin.common.entity.po.TInterviewRoundPO;
import com.zhuojy.equity.mall.admin.common.entity.po.TPositionMessagePO;
import com.zhuojy.equity.mall.admin.common.entity.vo.PositionMessageEntity;
import com.zhuojy.equity.mall.admin.common.entity.vo.TPositionMessageVO;
import com.zhuojy.equity.mall.admin.common.pojo.JsonResult;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.dal.mapper.PositionMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.message.TInterviewRoundMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.message.TPositionMessageMapper;
import com.zhuojy.equity.mall.admin.manager.UserManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author:LYL
 * @Date 2024/4/9 19:47
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TPositionMessageService extends ServiceImpl<TPositionMessageMapper, TPositionMessagePO> {
    private final TInterviewRoundMapper tInterviewRoundMapper;
    private final PositionMapper positionMapper;
    @Transactional(rollbackFor = Exception.class)
    public void addPositionMessage(TPositionMessageBO tPositionMessageBO) {
        TPositionMessagePO tPositionMessagePO = new TPositionMessagePO();
        BeanUtils.copyProperties(tPositionMessageBO,tPositionMessagePO);
        this.baseMapper.insert(tPositionMessagePO);
        TPosition tposition = tPositionMessageBO.getTposition();
        tposition.setMessageId(tPositionMessagePO.getId());
        positionMapper.insert(tposition);
        String[] tinterview = tPositionMessageBO.getTinterview();

        for (String s : tinterview) {
            String[] split = s.split(",");
            TInterviewRoundPO roundPO = new TInterviewRoundPO();
            for (int i = 0; i < split.length; i++) {
                roundPO.setPositionId(tPositionMessagePO.getId());
                roundPO.setInterviewer(Integer.parseInt(split[i]));
                roundPO.setConferenceRoom(Integer.parseInt(split[i]));
            }
            tInterviewRoundMapper.insert(roundPO);
            roundPO.setId(null);
        }

    }

    public JsonResult findPositionMessageFenYe(TPositionMessageVO tPositionMessageVO, Integer pageNum, Integer pageSize) {
        Page<TPositionMessageVO> page = new Page<>(pageNum, pageSize);
        IPage<TPositionMessageVO> positionMessageFenYe = this.baseMapper.findPositionMessageFenYe(tPositionMessageVO, page);
        return JsonResult.success(positionMessageFenYe.getRecords(), positionMessageFenYe.getTotal());
    }

    public void updateStatusById(TPositionMessageVO tPositionMessageVO) {
        this.baseMapper.updateStatusById(tPositionMessageVO);
    }

    public void delPositionMessageById(Long id) {
        this.baseMapper.deleteById(id);
        this.baseMapper.delById(id);
    }

    public TPositionMessageVO findPositionMessageById(Long id) {
        return this.baseMapper.findPositionMessageById(id);
    }

    public List<TInterviewRoundPO> findInterviewRound(Long id) {
        return this.baseMapper.findInterviewRound(id);
    }

    public void updateTPositionMessage(TPositionMessageVO tPositionMessageVO) {
        TPositionMessagePO tPositionMessagePO = new TPositionMessagePO();
        BeanUtils.copyProperties(tPositionMessageVO,tPositionMessagePO);
        this.baseMapper.updateById(tPositionMessagePO);
        TPosition tposition = tPositionMessageVO.getTposition();
        positionMapper.updateById(tposition);
        TInterviewRoundPO tinterviewRoundPO = tPositionMessageVO.getTinterviewRoundPO();
        tInterviewRoundMapper.updateById(tinterviewRoundPO);
    }

    public void getPositionMessageEntity(HttpServletResponse response) {
        List<PositionMessageEntity> positionMessageEntity = this.baseMapper.getPositionMessageEntity();
        ExcelUtils.export(response, "导出招聘列表", positionMessageEntity, PositionMessageEntity.class);
    }

    public List<TPositionMessageVO> findPMessageAll() {
      return this.baseMapper.findPMessageAll();
    }
}
